﻿//***************获取Table中的数据，并提供Xml内容的支持类***********
//获取指定id对应的Table对象
function TableXmlEngen(oTab) {
    var oTable;
    if (!oTab) {//如果不存在oTab
    }
    else {
        oTable = oTab;
    }
    this.Init = function(oTab) {
        oTable = oTab;
    };
    //返回属性满足要求的所有行的数组对象
    this.getRows = function(oTable, sJuageAttr, sFitValue) {
        var nIndex = 0;
        var oRows = new Array();
        for (nIndex = 0; nIndex < oTable.rows.length; nIndex++) {
            if (oTable.rows[nIndex].getAttribute(sJuageAttr) == sFitValue) {
                //add to array
                oRows.push(oTable.rows[nIndex]);
            }
            else if (!sJuageAttr || !sFitValue) {
                //如果第二、三任何一个数据不存在，则表示不过滤
                oRows.push(oTable.rows[nIndex]);
            }
        }
        return oRows;
    };
    //返回一行的所有的列的属性值
    //依赖于：getUseAttributes
    this.getRowProperty = function(oRow) {
        var oPropertys = [];
        var nIndex = 0;
        for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) {
            oPropertys.push(getUseAttributes(oRow.cells[nIndex]));
        }
        return oPropertys;
    };
    //返回的是键值对的集合,name:value;
    //函数返回所有的有意义的属性和属性值，默认的空白属性和属性值都不会返回
    this.getUseAttributes = function(oUnit, arrAttrList) {
        var nIndex = 0;
        var oAttrs = [];
        var oTmpData = {};
        if (!arrAttrList) {
            for (nIndex = 0; nIndex < oUnit.attributes.length; nIndex++) {
                var oAttribute = oUnit.attributes[nIndex];
                if (oAttribute.value != "null" && !oAttribute.value == false) {
                    oTmpData = { name: oAttribute.name, value: oAttribute.value };
                    oAttrs.push(oTmpData);
                }
            }
        }
        else {
            for (nIndex = 0; nIndex < arrAttrList.length; nIndex++) {
                var sValue = oUnit.getAttribute(arrAttrList[nIndex]);
                if (!sValue) {

                }
                else {
                    oTmpData = { name: arrAttrList[nIndex], value: sValue };
                    oAttrs.push(oTmpData);
                }
            }
        }
        return oAttrs;
    };
    //获取一个单元中的有效数据，如果有多个，取第一个
    this.getCellValue = function(oCell) {
        var nIndex = 0;
        if (oCell.children.length > 0) {
            for (nIndex = 0; nIndex < oCell.children.length; nIndex++) {
                //分情况讨论
                var oCellChild = oCell.children[nIndex];
                var sNodeType = oCellChild.tagName;
                switch (String(sNodeType).toLowerCase()) {
                    case 'input':
                        {
                            //判断input是什么类型
                            var sType = oCellChild.getAttribute("type");
                            sType = String(sType).toLowerCase();
                            switch (sType) {
                                case 'text':
                                    {
                                        if (oCellChild.getAttribute("issave") == "true") {
                                            if (oCellChild.getAttribute("fromvalue") == "true") {
                                                return oCellChild.getAttribute("value");
                                            }
                                            else {
                                                return oCellChild.text;
                                            }
                                        }
                                        break;
                                    }
                                case 'radio':
                                    {
                                        if (oCellChild.getAttribute("issave") == "true") {
                                            if (oCellChild.checked) {
                                                return "true";
                                            }
                                            else {
                                                return "false"
                                            }
                                        }
                                        break;
                                    }
                                case 'checkbox':
                                    {
                                        if (oCellChild.status == true) {
                                            return "true";
                                        }
                                        else {
                                            return "false";
                                        }
                                        break;
                                    }
                            }
                            break;
                        }
                    case 'select':
                        {
                            //下拉框时，自动获取轩中行
                            var sSelectedValue = oCellChild[oCellChild.selectedIndex].text
                            return sSelectedValue;
                            break;
                        }
                    default:
                        {
                            return oCellChild.innerHTML;
                        }
                }

            }
            //程序执行到这里，说明没有一个数据得到保存，于是返回空文本
            return "";
        }
        else {
            //没有children，说明是普通的表格内容
            return oCell.innerHTML;
        }
    }
    this.getRowData = function(oRow, arrAttrGet) {
        var oRowData = [];
        var nIndex = 0;
        var arrAttributes = this.getUseAttributes(oRow, arrAttrGet);
        //先获取一些属性

        for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) {
            if (!oRow.cells[nIndex].getAttribute("name")) {
                var oDic = { name: "name" + nIndex, value: oRow.cells[nIndex].innerHTML };
                oRowData.push(oDic);
            }
            else {
                var oDic = { name: oRow.cells[nIndex].getAttribute("name"), value: this.getCellValue(oRow.cells[nIndex]) };
                oRowData.push(oDic);
            }
        }
        oRowData.appendData = function(oXml, oTopNode) {
            var oNewNode = oXml.createElement("data");
            //give this node its attributes

            var nIndex = 0;
            for (nIndex = 0; nIndex < arrAttributes.length; nIndex++) {
                oNewNode.setAttribute(arrAttributes[nIndex].name, arrAttributes[nIndex].value);
            }
            oTopNode.appendChild(oNewNode);
            for (nIndex = 0; nIndex < oRowData.length; nIndex++) {
                var oNode = oXml.createElement("data-collum");
                var oNodeText = oXml.createTextNode(oRowData[nIndex].value);
                oNode.setAttribute("name", oRowData[nIndex].name);
                oNode.appendChild(oNodeText);
                oNewNode.appendChild(oNode);
            }
            return;
        };
        return oRowData;
    };

    this.getTableData = function(sRowType, sTypeValue, arrAttrGet) {
        var nIndex = 0;
        var oTableInfo = [];
        var oRows = this.getRows(oTable, sRowType, sTypeValue);
        for (nIndex = 0; nIndex < oRows.length; nIndex++) {
            oTableInfo.push(this.getRowData(oRows[nIndex], arrAttrGet));
        }
        oTableInfo.createXmlObj = function() {
            var sXml = "";
            var nIndex = 0;
            var oXml = new ActiveXObject("Microsoft.XMLDOM");   //先创建好一个空的Xml对象
            var oTopNode = oXml.createElement("SunSoft_Data");  //创建一个顶级节点
            oXml.appendChild(oTopNode);                         //将这个顶级节点加到Xml对象上
            //oTopNode是顶级Node（节点）
            for (nIndex = 0; nIndex < oTableInfo.length; nIndex++) {
                oTableInfo[nIndex].appendData(oXml, oTopNode);  //为顶级节点增加子节点，每个子节点代表一行的数据，调用的是oRowData.appendData方法
            }
            return oXml;
        }
        return oTableInfo;
    };
}